/**
 * Copyright (c) The openTCS Authors.
 *
 * This program is free software and subject to the MIT license. (For details,
 * see the licensing information (LICENSE.txt) you should have received with
 * this copy of the software.)
 */
package org.opentcs.virtualvehicle;

import com.google.inject.assistedinject.FactoryModuleBuilder;
import org.opentcs.customizations.kernel.KernelInjectionModule;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import zdh.fsy.jjwcVehicle.JJWCLoopAdapterComponentsFactory;
import zdh.fsy.jjwcVehicle.JJWCLoopCommunicationAdapterFactory;
import zdh.fsy.jjwcVehicle.JJWCLoopVehicleConfiguration;

/**
 * Configures/binds the loopback communication adapters of the openTCS kernel.
 */
public class LoopbackCommAdapterModule
    extends KernelInjectionModule {

  /**
   * This class's logger.
   */
  private static final Logger LOG = LoggerFactory.getLogger(LoopbackCommAdapterModule.class);

  /**
   * Creates a new instance.
   */
  public LoopbackCommAdapterModule() {
  }

  @Override
  protected void configure() {
    jjwcRxxVehicle();
    virtualVehicle();
  }

  /**
   * 这是初始化捷佳伟创睿心行的适配器
   */
  private void jjwcRxxVehicle()
  {
    JJWCLoopVehicleConfiguration configuration
        = getConfigBindingProvider().get(JJWCLoopVehicleConfiguration.PREFIX,
                                         JJWCLoopVehicleConfiguration.class);
    if (!configuration.enable()) {
      LOG.info("JJWC_Loopback driver disabled by configuration.");
      return;
    }

    bind(JJWCLoopVehicleConfiguration.class)
        .toInstance(configuration);

    install(new FactoryModuleBuilder().build(JJWCLoopAdapterComponentsFactory.class));

    // tag::documentation_createCommAdapterModule[]
    vehicleCommAdaptersBinder().addBinding().to(JJWCLoopCommunicationAdapterFactory.class);
    // end::documentation_createCommAdapterModule[]
  }

  /**
   * 这是初始化仿真小车的适配器
   */
  private void virtualVehicle()
  {
    VirtualVehicleConfiguration configuration
        = getConfigBindingProvider().get(VirtualVehicleConfiguration.PREFIX,
                                         VirtualVehicleConfiguration.class);

    if (!configuration.enable()) {
      LOG.info("Loopback driver disabled by configuration.");
      return;
    }

    bind(VirtualVehicleConfiguration.class)
        .toInstance(configuration);

    install(new FactoryModuleBuilder().build(LoopbackAdapterComponentsFactory.class));

    // tag::documentation_createCommAdapterModule[]
    vehicleCommAdaptersBinder().addBinding().to(LoopbackCommunicationAdapterFactory.class);
    // end::documentation_createCommAdapterModule[]
  }

}
